package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.Paint
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.appcompat.widget.AppCompatTextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.ShopCarListResponse
import com.gexiaobao.pigeon.app.util.ImageLoaderManager
import com.gexiaobao.pigeon.app.util.SettingUtil
import com.gexiaobao.pigeon.databinding.ItemShoppingCartGoodsBinding

/**
 * @Author      : hxw
 * @Date        : 2023/6/28 13:21
 * @Describe    :
 */
class GoodsShoppingCartAdapter(data: ArrayList<ShopCarListResponse.ListBean.ListGoodsBean>) :
    BaseQuickAdapter<ShopCarListResponse.ListBean.ListGoodsBean, DataBindBaseViewHolder>(R.layout.item_shopping_cart_goods, data) {

    @SuppressLint("NotifyDataSetChanged")
    override fun convert(holder: DataBindBaseViewHolder, item: ShopCarListResponse.ListBean.ListGoodsBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemShoppingCartGoodsBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        val imageGoodsIcon = holder.getView<ImageView>(R.id.ivGoodsImage)
        val tvGoodsPrice = holder.getView<AppCompatTextView>(R.id.tvGoodsPrice)
        ImageLoaderManager.loadRoundImageOrEmpty(context, item.specImg, imageGoodsIcon, 15, R.drawable.shop_product_def)

        val tvGoodsPriceShow = holder.getView<AppCompatTextView>(R.id.tvGoodsPriceShow)
        tvGoodsPriceShow.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG; //中划线
//        /** 计算价格：单价x数量=总价 */
//        val price = Util.mul(item.finalPrice.toString(), item.buyCount.toString()).toDouble()
        val finalPrice = SettingUtil.div(item.finalPrice, 100.00, 2).toString()
        SettingUtil.setPrice(finalPrice, tvGoodsPrice, 55, Color.BLACK)
        val goods = data[getItemPosition(item)]
        if (goods.check == 1) {
            holder.setImageResource(R.id.ivShopCarGoodsChecked, R.mipmap.icon_good_check)
        } else {
            holder.setImageResource(R.id.ivShopCarGoodsChecked, R.mipmap.icon_good_uncheck)
        }
        if (holder.adapterPosition == data.size - 1) {
            holder.setGone(R.id.viewLine, true)
        } else {
            holder.setGone(R.id.viewLine, false)
        }

        if (item.isInvalid == 1) {//已失效
            holder.setEnabled(R.id.ivShopCarGoodsChecked, false)
            holder.setGone(R.id.llLoseEfficacy, false)
            holder.setGone(R.id.tvLoseEfficacy, false)
        } else {
            holder.setEnabled(R.id.ivShopCarGoodsChecked, true)
            holder.setGone(R.id.llLoseEfficacy, true)
            holder.setGone(R.id.tvLoseEfficacy, true)
        }

        val llLoseEfficacy = holder.getView<LinearLayout>(R.id.llLoseEfficacy)
        llLoseEfficacy.setOnClickListener { }

        holder.itemView.apply {
            binding.swipeRecyclerViewCoupon.adapter = null
            if (item.isEmpty()) {
                holder.setGone(R.id.swipeRecyclerViewCoupon, true)//隐藏
                return
            }
            holder.setGone(R.id.swipeRecyclerViewCoupon, false)//显示
            val couponAdapter = CouponShopCarAdapter(item.useCoupon)
            binding.swipeRecyclerViewCoupon.apply {
                layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
                adapter = couponAdapter
                couponAdapter.notifyDataSetChanged()
            }
        }
    }
}